﻿Imports System.Data.OleDb

Public Class EditProfile
    Inherits System.Web.UI.Page

    Dim isFirstTime As Boolean = True

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
            Dim selectUserSQL As String = "SELECT * FROM UserProfile WHERE Username=@f1"
            Dim selectUserCmd As OleDbCommand = New OleDbCommand(selectUserSQL, oleDbConn)
            selectUserCmd.CommandType = CommandType.Text
            selectUserCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim selectUserDr As OleDbDataReader = selectUserCmd.ExecuteReader()

            If selectUserDr.HasRows Then
                selectUserDr.Read()
                If Not IsDBNull(selectUserDr("FirstName")) Then txtFirstName.Text = selectUserDr("FirstName")
                If Not IsDBNull(selectUserDr("LastName")) Then txtLastName.Text = selectUserDr("LastName")
                If Not IsDBNull(selectUserDr("DateOfBirth")) Then txtDateOfBirth.Text = selectUserDr("DateOfBirth")
                If Not IsDBNull(selectUserDr("Address")) Then txtAddress.Text = selectUserDr("Address")
                If Not IsDBNull(selectUserDr("CountryID_FK")) Then ddlCountry.SelectedValue = selectUserDr("CountryID_FK")
                If Not IsDBNull(selectUserDr("EmailAddress")) Then txtEmailAddress.Text = selectUserDr("EmailAddress")
                If Not IsDBNull(selectUserDr("FavouriteClub_FK")) Then ddlFavouriteClub.SelectedValue = selectUserDr("FavouriteClub_FK")
            End If

            selectUserCmd.Dispose()
            oleDbConn.Close()
            oleDbConn.Dispose()
        End If

    End Sub

    Protected Sub btnSaveChanges_Click(sender As Object, e As EventArgs) Handles btnSaveChanges.Click
        If Page.IsValid Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
            oleDbConn.Open()

            Dim isFirstTimeSQL As String = "SELECT FirstName FROM UserProfile WHERE Username=@f1"
            Dim isFirstTimeCmd As OleDbCommand = New OleDbCommand(isFirstTimeSQL, oleDbConn)
            isFirstTimeCmd.CommandType = CommandType.Text
            isFirstTimeCmd.Parameters.AddWithValue("@f1", User.Identity.Name)

            Dim isFirstTimeDr As OleDbDataReader = isFirstTimeCmd.ExecuteReader()

            If isFirstTimeDr.HasRows Then
                isFirstTimeDr.Read()
                If Not IsDBNull(isFirstTimeDr("FirstName")) Then
                    isFirstTime = False
                End If
            End If

            isFirstTimeCmd.Dispose()

            Dim newFileName As String = ""

            If fuPicture.HasFile Then
                Dim myGUID As New System.Guid
                myGUID = Guid.NewGuid
                newFileName = myGUID.ToString + ".jpg"
                Dim picLocationOnServerHardDisk = Request.MapPath("Profiles") & "/" & newFileName
                fuPicture.SaveAs(picLocationOnServerHardDisk)
            ElseIf isFirstTime Then
                newFileName = "profile.jpg"
            End If

            Dim updateUserSQL As String

            If fuPicture.HasFile Or isFirstTime Then
                updateUserSQL = "UPDATE UserProfile SET FirstName=@f1,LastName=@f2,DateOfBirth=@f3,Address=@f4,CountryID_FK=@f5,EmailAddress=@f6,FavouriteClub_FK=@f7,UpdatedAt=@f8,Picture=@f9 WHERE Username=@f10"
            Else
                updateUserSQL = "UPDATE UserProfile SET FirstName=@f1,LastName=@f2,DateOfBirth=@f3,Address=@f4,CountryID_FK=@f5,EmailAddress=@f6,FavouriteClub_FK=@f7,UpdatedAt=@f8 WHERE Username=@f10"
            End If

            Dim updateUserCmd As OleDbCommand = New OleDbCommand(updateUserSQL, oleDbConn)
            updateUserCmd.CommandType = CommandType.Text
            updateUserCmd.Parameters.AddWithValue("@f1", txtFirstName.Text)
            updateUserCmd.Parameters.AddWithValue("@f2", txtLastName.Text)
            updateUserCmd.Parameters.AddWithValue("@f3", txtDateOfBirth.Text)
            updateUserCmd.Parameters.AddWithValue("@f4", txtAddress.Text)
            updateUserCmd.Parameters.AddWithValue("@f5", ddlCountry.SelectedValue)
            updateUserCmd.Parameters.AddWithValue("@f6", txtEmailAddress.Text)
            updateUserCmd.Parameters.AddWithValue("@f7", ddlFavouriteClub.SelectedValue)
            updateUserCmd.Parameters.AddWithValue("@f8", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))

            If fuPicture.HasFile Or isFirstTime Then
                updateUserCmd.Parameters.AddWithValue("@f9", "Profiles/" & newFileName)
            End If

            updateUserCmd.Parameters.AddWithValue("@f10", User.Identity.Name)
            updateUserCmd.ExecuteNonQuery()

            System.Threading.Thread.Sleep(500)
            Session("UserMessage") = "Your profile was successfully updated!"
            Response.Redirect("UserProfile.aspx?Username=" & User.Identity.Name)

            updateUserCmd.Dispose()
            oleDbConn.Close()
            oleDbConn.Dispose()
        Else
            Session("UserMessage_Error") = "There was a problem with the data you submitted. Please try again."
            Response.Redirect(Request.RawUrl)
        End If
    End Sub

    Protected Sub btnBackProfile_Click(sender As Object, e As EventArgs) Handles btnBackProfile.Click
        Response.Redirect("~/UserProfile.aspx?Username=" & User.Identity.Name)
    End Sub

End Class